/*
 * Copyright (C), 2014-2016, org.esaydev
 * FileName: BaseWechatResult.java
 * Author:   jian
 * Date:     2016年3月21日 
 */
package org.easydev.wechat.client.result.base;

import org.easydev.wechat.common.constants.WechatErrorCode;
import org.easydev.wechat.common.exception.WechatException;

import com.alibaba.fastjson.util.TypeUtils;

/**
 * <Description> 微信调用返回结果<br>
 */
public class BaseWechatResult {

    /**
     * success
     */
    private boolean success = false;

    /**
     * 
     */
    private String errorCode = WechatErrorCode.SYSTEM_ERROR.getCode();

    /**
     * 
     */
    private String errorMsg = WechatErrorCode.SYSTEM_ERROR.getMessage();

    /**
     * 
     */
    private Object data;

    /**
     * get success
     * 
     * @return Returns the success.<br>
     */
    public boolean isSuccess() {
        return success;
    }

    /**
     * set success
     * 
     * @param success The success to set. <br>
     */
    public void setSuccess(boolean success) {
        this.success = success;
    }

    /**
     * get errorCode
     * 
     * @return Returns the errorCode.<br>
     */
    public String getErrorCode() {
        return errorCode;
    }

    /**
     * set errorCode
     * 
     * @param errorCode The errorCode to set. <br>
     */
    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    /**
     * get errorMsg
     * 
     * @return Returns the errorMsg.<br>
     */
    public String getErrorMsg() {
        return errorMsg;
    }

    /**
     * set errorMsg
     * 
     * @param errorMsg The errorMsg to set. <br>
     */
    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

    /**
     * get data
     * 
     * @return Returns the data.<br>
     */
    public Object getData() {
        return data;
    }

    /**
     * set data
     * 
     * @param data The data to set. <br>
     */
    public void setData(Object data) {
        if (null != data) {
            this.success = true;
        }
        this.data = data;
    }

    /**
     * Description: 设置错误信息<br>
     * 
     * @param errorCodeEm <br>
     */
    public void setErrorCode(WechatErrorCode errorCodeEm) {
        this.errorCode = errorCodeEm.getCode();
        this.errorMsg = errorCodeEm.getMessage();
    }

    /**
     * Description: 设置错误信息<br>
     * 
     * @param errorCodeEm <br>
     *            WechatException
     */
    public void setErrorCode(WechatException error) {
        this.errorCode = error.getErrorCode();
        this.errorMsg = error.getMessage();
    }

    /**
     * get data
     * 
     * @return Returns the data.<br>
     */
    public <T> T getData(Class<T> cls) {
        return TypeUtils.castToJavaBean(data, cls);
    }
}
